package pub.tbc.api.service;

import io.netty.handler.codec.http.DefaultFullHttpRequest;
import io.netty.handler.codec.http.FullHttpRequest;
import io.netty.handler.codec.http.HttpMethod;
import io.netty.handler.codec.http.HttpVersion;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import pub.tbc.api.helper.ServiceHelper;

/**
 * 定义一个服务
 *
 * @author tbc on 2017/3/18.
 */
@Slf4j
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class ServiceInfo {
    private String serviceName;
    private String requestUri;//请求路径-支持ant表达式
    //    private String mapUri;
    private String contentPath; // 如果有该字段
    private String httpMethod;
    private boolean isLogin;//是否需要登陆
    private boolean isEmpower;//访问该服务是否需要授权
    private int serviceStatus;//服务状态：可用、不可用
    private String protocolType;//协议类型，如http/rmi等

    public void setIsLogin(boolean isLogin){
        this.isLogin = isLogin;
    }

    public void setIsEmpower(boolean isEmpower){
        this.isEmpower = isEmpower;
    }

    public static ServiceInfo defaultServiceInfo(FullHttpRequest request) {
        String serviceName = ServiceHelper.getServiceNameByUri(request.uri());
        return builder()
                .serviceName(serviceName)
                .requestUri(request.uri())
                .contentPath("")
                .httpMethod(request.method().name())
                .isLogin(false)
                .isEmpower(false)
                .serviceStatus(1)
                .protocolType("http")
                .build();
    }


    public static void main(String[] args) {

        System.out.println("demo".indexOf("/"));

        FullHttpRequest request = new DefaultFullHttpRequest(
                HttpVersion.HTTP_1_1,
                HttpMethod.GET,
                "demo/v1"
        );
        System.out.println(defaultServiceInfo(request));


    }

}
